import { Role } from '@/auth/enum';
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { user } from '@prisma/client';
import { Observable } from 'rxjs';

@Injectable()
export class RoleGuard implements CanActivate {
  constructor(private reflector: Reflector) {}
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    const user = request.user as user
    console.log(user);
    // context.getHandler()  获取到方法
    // context.getClass()  获取到控制器
    const roles = this.reflector.getAllAndMerge<Role[]>('roles', [context.getHandler(),context.getClass()]);
    console.log(roles);
    return roles.length ? roles.some(role=>user.rule == role) :true
    
  
    
    return false
  }
}
